package net.test.piggy.controller;

import com.alibaba.fastjson.JSONObject;
import net.test.piggy.po.ChatMessage;
import net.test.piggy.model.po.UserPO;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class MongoController {
    @Resource
    private MongoTemplate mongoTemplate;

    @ResponseBody
    @GetMapping("mongo01")
    public ChatMessage mongo01(){

        UserPO user = new UserPO();
        user.setId(1);
        user.setEnabled((short) 1);
        user.setPassword("password");
        user.setUsername("username");

        String jsonString = JSONObject.toJSONString(user);
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setInfo(jsonString);
        chatMessage.setUserId("userId");
        ChatMessage a = mongoTemplate.save(chatMessage);
        return mongoTemplate.findById(a.getId(), ChatMessage.class);
    }
}
